package br.com.moto.ejb;
 
import br.com.moto.entity.Album;
import java.util.List;
 
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
 
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
 
@Stateless
@LocalBean
public class AlbumSessionBean {

    @PersistenceContext
    private EntityManager em;    

    public List<Album> retrieve() {
        Query query = em.createNamedQuery("Album.findAll");
        return query.getResultList();
    }
    
    public Album getAlbum(Album album){
        Query query = em.createNamedQuery("Album.findById").setParameter("id", album.getId());
        return (Album) query.getSingleResult();
    }    
    
    public List<Album> getAlbumToUser(String username) {
        Query query = em.createNamedQuery("Album.findByUsername").setParameter("email", username);
        return query.getResultList();
    }    
    
    public void delete(Album album) {
        Album excluir = em.find(Album.class, album.getId());
        em.remove(excluir);
    }    
        
     public Album create(Album album)
     {
        em.merge(album);          
        return album;
    }    

    public Album update(Album album)
    {
           
        Album updated = em.merge(album);        
        System.out.println("Album atualizado no bean");
        return updated;
    }
}
